
<template>
  <div class="execution">
    <basic-container>
      <avue-crud
        v-model="form"
        v-bind="bindVal"
        v-on="onEvent"
        :page.sync="page"
        :permission="permissionList"
      >
        <template slot="menuLeft">
          <el-popover v-if="shenpi_btn" placement="bottom" width="260" v-model="batchReviewVisible">
            <div style="text-align: center; margin: 0">
              <el-button size="mini" type="primary" @click="auditO(2)">审批通过</el-button>
              <el-button type="primary" size="mini" @click="auditO(3)">审批不通过</el-button>
            </div>
            <el-button type="primary" size="small"  slot="reference">批量审批</el-button>
          </el-popover>

          <el-button type="primary" v-if="tijiao_btn" size="small" @click="submitO">提交</el-button>
          <el-button type="primary" size="small"
            v-permission="permissionList.delBatchBtn"
            @click="handleBatchDel">删除</el-button>
            <el-upload
                  class="upload-inline"
                  action=""
                  :show-file-list="false"
                  accept=".xls,.xlsx"
                  :http-request="handleImport"
                  v-if="daoru_btn">
                  <el-button type="primary" size="small">导入</el-button>
                </el-upload>
          <el-button type="primary" size="small" @click="handleExport">导 出</el-button>
        </template>
      </avue-crud>
    </basic-container>
  </div>
</template>

<script>
import baseCrud from '@/mixins/base-crud'
import {
  fetchList,
  getObj,
  addObj,
  putObj,
  delObj,
  submitBatch,
  auditBatch,
  delBatch,
  exportExcel,
  saveBatch
} from "@/api/subway/swtkblack";
import { tableOption,startDate, endDate } from "@/const/crud/subway/swtkblack";
import { mapGetters } from "vuex";

export default {
  name: "swtkblack",
  mixins: [baseCrud('subway/swtkblack')],
  data() {
    return {
      batchReviewVisible: false,
      form:{},
      searchForm: {
        dates1: [startDate, endDate]
      }
    };
  },
  // 自定义按钮权限初始化
  created() {
    this.shenpi_btn = this.permissions["st_swtkblack_auditBatch"];
    this.tijiao_btn = this.permissions["st_swtkblack_submitBatch"];
    this.daoru_btn = this.permissions["st_swtkblack_import"];
  },
  computed: {
    ...mapGetters(["permissions"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permissions.st_swtkblack_add, false),
        delBtn: this.vaildData(this.permissions.st_swtkblack_del, false),
        editBtn: this.vaildData(this.permissions.st_swtkblack_edit, false),
        delBatchBtn: this.vaildData(this.permissions.st_swtkblack_delBatch, false)
      };
    }
  },
  methods: {
     handleImport(item){
              let file = item.file
              let param = new FormData()
              param.append('file',file)
              saveBatch(param).then(() => {
                this.$notify({
                  title: '成功',
                  message: '导入成功',
                  type: 'success',
                  duration: 2000
                })
                this.getList(this.page)
              })
            },
    auditO(s) {
      let loadingInstance1 = this.$loading({
        lock: true,
        text: "Loading",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.7)"
      });
      if (this.$refs.crud.tableSelect.length < 1) {
        this.$message.error("请选择数据");
        loadingInstance1.close();
        return false;
      }
      let ids = [];
      for (const table of this.$refs.crud.tableSelect) {
        if (table.handleStatus != "1") {
          this.$message.error("请检查勾选项，只能审批未审核的数据。");
          loadingInstance1.close();
          return false;
        }
        table.handleStatus=s;
        ids.push(table);
      }
      auditBatch(ids)
        .then(data => {
          loadingInstance1.close();
          this.$message.success("审批成功");
          this.getList(this.page);
           this.batchReviewVisible = false;
        })
        .catch(() => {
          loadingInstance1.close();
        });
    },
    submitO() {
      if (this.$refs.crud.tableSelect.length < 1) {
        this.$message.info("请选择要提交的记录");
        return false;
      }
      let ids = [];
      for (const table of this.$refs.crud.tableSelect) {
        if (table.handleStatus != "0") {
          this.$message.error("请检查勾选项，只能提交未提交的数据。");
          return false;
        }
        ids.push(table);
      }
      submitBatch(ids)
        .then(data => {
          this.$message.success("提交成功");
          this.getList(this.page);
        })
    },
    handleBatchDel(){
      let tableSelect = this.$refs.crud.tableSelect
      if (tableSelect.length < 1) {
        this.$message.info("请选择要删除的记录");
        return false;
      }
      for(const item of tableSelect){
        if(item.handleStatus!=0){
          this.$message.info("只能删除未提交的数据！");
          return
        }
      }
      let ids = tableSelect.map(row => row.id)
      this.$confirm(`是否确认删除${ids.length}条记录`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return delBatch(ids)
      }).then(() => {
        this.$message.success('删除成功')
        this.getList(this.page)
      })

    },
    getList(page, params) {

      this.tableLoading = true;
      if(this.shenpi_btn){
         return fetchList(
        Object.assign(
          {
            current: page.currentPage,
            size: page.pageSize,
            roleSP:'0'
          },
          params,
          this.searchForm
        )
      )
        .then(response => {
          this.tableData = response.data.data.records;
          this.page.total = response.data.data.total;
          this.tableLoading = false;
        })
        .catch(() => {
          this.tableLoading = false;
        });
      }else{
          return fetchList(
        Object.assign(
          {
            current: page.currentPage,
            size: page.pageSize
          },
          params,
          this.searchForm
        )
      )
        .then(response => {
          this.tableData = response.data.data.records;
          this.page.total = response.data.data.total;
          this.tableLoading = false;
        })
        .catch(() => {
          this.tableLoading = false;
        });
      }

    },
  }
};
</script>
